using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace MenuScreen
{
    
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;

        enum gameState { PLAY, MENU, OPTION, EXIT, NEW_GAME };
        gameState gamestate = gameState.MENU;

        MouseState mouseState;
        MouseState prvmouseState;

        SpriteFont font;

        List<menuButton> lsmenuButton = new List<menuButton>();


        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);

            //graphics.PreferredBackBufferHeight = 620;
            //graphics.PreferredBackBufferWidth = 729;
            //graphics.IsFullScreen = true; //Lam lon man hinh.
            Content.RootDirectory = "Content";
            this.addButton();
        }

    
        protected override void Initialize()
        {
            IsMouseVisible = true;

            base.Initialize();
        }

        
        protected override void LoadContent()
        {
        
            spriteBatch = new SpriteBatch(GraphicsDevice);
            font = Content.Load<SpriteFont>("sFont");
            
        }

        protected override void UnloadContent()
        {
            
        }

        
        protected override void Update(GameTime gameTime)
        {
             
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            mouseState = Mouse.GetState();
            pressKey(mouseState);

            pressKey();
            base.Update(gameTime);
        }

         
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            spriteBatch.Begin();

            if(gamestate == gameState.MENU)
            {
                // Them code menu o day
                foreach (menuButton m in lsmenuButton)
                {
                    m.Draw(spriteBatch, font);                    
                }
            }

            spriteBatch.End();

            base.Draw(gameTime);
        }

        //
        public void addButton()
        {
            lsmenuButton.Add(new menuButton(new Rectangle(0, 145, 100, 60), "Play"));
            lsmenuButton.Add(new menuButton(new Rectangle(0, 220, 100, 60), "New Game"));
            lsmenuButton.Add(new menuButton(new Rectangle(0, 295, 100, 60), "Option"));
            lsmenuButton.Add(new menuButton(new Rectangle(0, 370, 100, 60), "Exit"));
        }

        //
        public void pressKey(MouseState mouse)
        {
            foreach (menuButton m in lsmenuButton)
            {
                m.mouseOver(mouseState);

                if (m.text == "Play" &&
                    mouse.LeftButton == ButtonState.Pressed &&
                    prvmouseState.LeftButton == ButtonState.Released &&
                    m.selected == true)
                    gamestate = gameState.PLAY;

                if (m.text == "Exit" &&
                    mouse.LeftButton == ButtonState.Pressed &&
                    prvmouseState.LeftButton == ButtonState.Released &&
                    m.selected == true)
                    gamestate = gameState.EXIT;

                if (m.text == "Option" &&
                    mouse.LeftButton == ButtonState.Pressed &&
                    prvmouseState.LeftButton == ButtonState.Released &&
                    m.selected == true)
                    gamestate = gameState.OPTION;

                if (m.text == "New Game" &&
                    mouse.LeftButton == ButtonState.Pressed &&
                    prvmouseState.LeftButton == ButtonState.Released &&
                    m.selected == true)
                    gamestate = gameState.NEW_GAME;

            }
            prvmouseState = mouse;
        }

        //
        public void pressKey()
        {
            switch (gamestate)
            {
                case gameState.EXIT:
                    this.Exit();
                    break;
                case gameState.OPTION:

                    Game game = new Game();
                    SetUp op = new SetUp(game);
                    game.RunOneFrame();        
            
                    break;
                case gameState.PLAY:
                    break;
                case gameState.NEW_GAME:
                    break;
            }
        }
    }
}
